package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Order;
import cn.wolfcode.domain.Product;
import cn.wolfcode.feign.IProductFeignApi;
import cn.wolfcode.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.UUID;

/**
 * @Description: 订单业务逻辑实现类
 * @Author: 伍家钰
 * @Date: 2020/8/28 18:39
 * @Version: 1.0
 */
@Service
@Slf4j
public class OrderServiceImpl implements IOrderService {
    /*// 调用Product(生产者)客户端的url地址,缺点是路径写死了,无法使用负载均衡,调用多个服务器
    private final String PRODUCT_URL = "http://localhost:8080";*/

    /**
     * 开启负载均衡,直接传入product在注册服务器中的key:PRODUCT-SERVER通过key获取到product在注册服务器中
     * 所有客户端的ip和端口
     */
    private final String PRODUCT_URL = "http://PRODUCT-SERVER";

    /* @Autowired
     private RestTemplate restTemplate;*/
    @Autowired
    private IProductFeignApi productFeignApi;

    @Override
    public Order save(Long userId, Long productId) {

        //远程获取,调用product客户端服务器
        //Product product = restTemplate.getForObject(PRODUCT_URL + "/products/get/" + productId, Product.class);
        Product product = productFeignApi.get(productId);
        Order order = new Order();
        order.setOrderNo(UUID.randomUUID().toString().replace("-", ""));
        order.setCreateTime(new Date());
        order.setUserId(userId);
        order.setProductName(product.getName());
        order.setProductPrice(product.getPrice());
        System.out.println("执行保存订单操作");
        //记录调用日志
        log.info("OrderController-save.....");
        return order;
    }
}
